#include "StateLevelSelect.h"

#include "userStates.h"

StateLevelSelect::StateLevelSelect()
{
    levelMenu.setMenuStart(50,50);
    levelMenu.loadFont("fonts/unispace.ttf", 24);
    levelMenu.setTextColour(WHITE);
    levelMenu.setTextSelectionColour(RED);

    GFX::setClearColour(BLACK);
}

StateLevelSelect::~StateLevelSelect()
{
    vector<DataState*>::iterator I;
    for (I = levelList.begin(); I < levelList.end(); ++I)
        delete (*I);
}

void StateLevelSelect::init()
{
    levelMenu.addItem(MENU_STRING_ITEM);
    levelMenu.setMenuItemText("---");
    levelMenu.setIsSelectable(false);
    levelMenu.addItem(MENU_STRING_ITEM);
    levelMenu.setMenuItemText("Back");
}

void StateLevelSelect::userInput()
{
    input->update();

    if (input->isUp())
        levelMenu.menuUp();
    else if (input->isDown())
        levelMenu.menuDown();

    if (input->isA())
    {
        if (levelMenu.getSelection() < levelMenu.size()-1)
            setNextState(STATE_DUMMY + levelMenu.getSelection() + 1);
        else
            setNextState(STATE_MAIN);
    }
    else if (input->isB())
    {
        setNextState(STATE_MAIN);
    }

    input->resetKeys();
}

void StateLevelSelect::update()
{
    levelMenu.update();
}

void StateLevelSelect::render()
{
    GFX::clearScreen();

    levelMenu.render();
}

void StateLevelSelect::addLevel(DataState* newLevel)
{
    levelList.push_back(newLevel);
    levelMenu.addItem(MENU_STRING_ITEM);
    levelMenu.setMenuItemText(newLevel->name);
}
